<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="title"
    :visible.sync="dialogFormVisible"
    append-to-body
    width="800px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="105px">
      <el-row :gutter="10">
        <el-col :lg="10" :md="24" :sm="24" :xl="10" :xs="24">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" :disabled="form.isDefault===1" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>


        <el-col :lg="6" :md="12" :sm="12" :xl="6" :xs="12">
          <el-form-item label="是否默认" prop="value">
            <el-switch
              v-model="form.isDefault"
              :active-value="1"
              :disabled="form.isDefault===1&&form.id!=null"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
        </el-col>

        <el-col :lg="8" :md="12" :sm="12" :xl="8" :xs="12">
          <el-form-item label="参数类型" prop="value">
            <el-select v-model="form.type" :disabled="form.isDefault===1" placeholder="请选择">
              <el-option
                :value="0"
                label="文本">
              </el-option>
              <el-option
                :value="1"
                label="图片">
              </el-option>
              <el-option
                :value="3"
                label="富文本">
              </el-option>
              <el-option
                :value="4"
                label="开关">
              </el-option>
              <el-option
                :value="5"
                label="数组">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>


        <el-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24">
          <el-form-item label="备注" prop="value">
            <el-input v-model="form.remarks" :disabled="form.isDefault===1" autocomplete="off"
                      type="textarea"></el-input>
          </el-form-item>
        </el-col>

        <!--    参数类型、0文字，1图片，3富文本，4开关，5文字数组-->

        <el-col v-if="form.type===0" :lg="24" :md="24" :sm="24" :xl="24" :xs="24">
          <el-form-item label="值" prop="value">
            <el-input v-model="form.value" autocomplete="off" type="textarea"></el-input>
          </el-form-item>
        </el-col>

        <el-col v-if="form.type===1" :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <UploadImage ref="UploadImage" :imgageUrl="form.value" @returnImgUrl="returnImgUrl"></UploadImage>
        </el-col>

        <el-col v-if="form.type===3" :lg="24" :md="24" :sm="24" :xl="24" :xs="24">
          <VabQuill
            ref="QuillEditor"
            v-model="form.value"
            :min-height="300"
            style="width: 100%;"
            @change="onEditorChange($event)"
          ></VabQuill>
        </el-col>
        <el-col v-if="form.type===4" :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="值" prop="value">
            <el-switch
              v-model="form.value"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col v-if="form.type===5" :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <div style="display:flex;">
            <el-tag
              v-for="(tag,index) in form.value.split(',')"
              :key="tag"
              :disable-transitions="false"
              closable
              @close="handleClose(index)">
              {{ tag }}
            </el-tag>
            <el-input
              v-if="inputVisible"
              ref="saveTagInput"
              v-model="inputValue"
              class="input-new-tag"
              size="small"
              @blur="handleInputConfirm"
              @keyup.enter.native="handleInputConfirm"
            >
            </el-input>
            <el-button v-else class="button-new-tag" size="mini" style="margin-left: 10px" @click="showInput">加一个
            </el-button>
          </div>
        </el-col>

      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>


  </el-dialog>
</template>

<script>
import {doInsert, doUpdate} from "@/api/shop/shopconfig/ShopConfigManagementApi";
import {isNull} from "@/utils/validate";
import {formateDate} from "@/utils/format";
import UploadImage from "@/views/modules/common/UploadImage";
import VabQuill from 'zx-quill'
import 'zx-quill/dist/zx-quill.css'
import {uploadJsonFile} from "@/api/commom";
import {getJson} from "@/api/tool/ossTool";

export default {
  components: {UploadImage, VabQuill},
  name: "ShopConfigManagementEdit",
  data() {

    return {
      MarkdownUrl: null,
      options: null,
      inputVisible: false,
      inputValue: '',
      form: {
        // 设置默认值
        type: 0,
        version: 0
      },
      baseForm: {
        locked: "0"
      },
      dict: {},
      rules: {
        name: [
          {required: true, trigger: "blur", message: "名称非空"},
        ],
      },
      title: "",
      dialogFormVisible: false,
    };
  },
  created() {

  },
  mounted() {
    // 加载字典值
  },
  methods: {
    handleClose(index) {
      console.log(index)
      let array = this.form.value.split(',')
      array.splice(index, 1);
      console.log(array)
      this.form.value = array.join();
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        if (isNull(this.form.value)) {
          this.form.value = inputValue;
        } else {
          this.form.value += ',' + inputValue;
        }
      }
      this.inputVisible = false;
      this.inputValue = '';
    },

    /**
     * 返回的图片地址
     * @param e
     */
    returnImgUrl(e) {
      console.log(e)
      this.form.value = e || ""
    },

    showEdit(row) {
      if (!row) {
        this.title = "添加";
      } else {
        this.title = "编辑";
        this.form = Object.assign({}, row);

        if (this.form.type === 3) {
          this.MarkdownUrl = this.form.value;
          this.getJson(this.MarkdownUrl)
        }
      }
      this.dialogFormVisible = true;
    },
    async getJson(url) {
      const {data} = await getJson(url);
      this.form.value = data

    },
    close() {
      this.dialogFormVisible = false;
      this.$refs["form"].resetFields();
      this.form = this.$options.data().form;
    },
    async save() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          // 处理数据
          this.handlerFormData(this.form);

          if (!isNull(this.form.type) && this.form.type === 3) {
            let {data} = await uploadJsonFile("json", this.form.value);
            this.form.value = data.fileStoragePath
          }

          // 修改
          if (!isNull(this.form.id)) {
            const {msg} = await doUpdate(this.form);
            this.$baseMessage(msg, "success");
          } else {
            const {msg} = await doInsert(this.form);
            this.$baseMessage(msg, "success");
          }

          await this.$emit("fetchData");
          this.close();
        } else {
          return false;
        }
      });
    },
    // 处理 form数据
    handlerFormData(formData) {
      if (!isNull(formData)) {
        for (let key in formData) {
          // 对于时间类进行处理
          if ("[object Date]" === Object.prototype.toString.call(formData[key])) {
            formData[key] = formateDate(formData[key], 'yyyy-MM-dd hh:mm:ss');
          }
        }
      }
    },
  },
};
</script>
